<?php
require_once "BaseAction.php";

class Event_BookmarkController extends TTEvent_Base_Action 
{
	
	public function init(){
		parent::init();
		$this->_helper->layout->setLayout('event-detail');
		$this->_helper->eventCommon->buildMainMenuBlock();
	}
	
	// bookmark list
	public function indexAction(){
		$eid = (int)$this->_getParam('eid', 0);
		if(!$eid)
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		
		$event = $this->_helper->modelLoader->getEventInfo($eid);
		if(empty($event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		}
        
        $this->setTitle("Bookmark | ".$event->event_name);
		
		if(!$this->_helper->privacy->isEnable('bookmark', $event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		$user = $this->getUser();
		$req = $this->getRequest();
		
		$isAdmin = $this->_helper->modelLoader->isEventAdmin($eid, $user->uid);
		if ($req->isPost() && $isAdmin)
		{
			$bookmark_delete = $req->getPost('bookmark_delete', 0);
			$bids = $req->getPost('bids', array());
			if ($bookmark_delete==1)
			{
				if(!empty($bids)){
					$this->delEventBookmarks($eid, $bids);
					$event = $this->_helper->modelLoader->getEventInfo($eid, false, false);
					$this->view->removeSuccess = 1;
					$this->view->removeCount = count($bids);
				}
			}
		}
		$page = (int)$req->getQuery('page', 0);
		$limit = 20;
		
		
		
		$totalBookmark = $this->_helper->bookmark->getEventBookmarkCount($eid);
		$totalPage = ceil($totalBookmark/$limit);
		if($page >= $totalPage)
			$page = max($totalPage-1, 1);

		$eventBookmarks = $this->_helper->bookmark->getEventBookmarks($eid, $page+1, $limit);
		
		$pageInfo = array();
		$pageInfo['total_item'] = $totalBookmark;
		$pageInfo['total_page'] = ceil($totalBookmark / $limit);
		$pageInfo['page'] = $page;
		$pageInfo['limit'] = $limit;
		$pageInfo['start'] = $page*$limit;
		
		//var_dump($pageInfo);exit();
		
		$this->view->bookmarks = $eventBookmarks;
		$this->view->totalBookmark = $totalBookmark;
		$this->view->limit = $limit;
		$this->view->uid = $user->uid;
		$this->view->pager = $this->view->partial("pager.phtml", array('pageInfo'=>$pageInfo, "url" => "/event/bookmark/" . $event->eid, "ext_param" => "", 'title'=>'bookmark'));
		$this->view->event = $event;
		$this->view->page = $page;
		
		$this->view->isAdmin  = $isAdmin;
		$this->view->action = 'list';
		$this->view->htmlLeftCol = $this->_helper->eventCommon->buildHtmlEventLeftCol($eid);
		
	}
	
	public function delEventBookmarks($eid, $bids){
		$eid = (int)$eid;
		
		for($i=0;$i<count($bids); $i++){
			$this->_helper->bookmark->deleteEventBookmark($bids[$i], $eid);
		}
		
		$total = $this->_helper->bookmark->getEventBookmarkCount($eid);
		$eventModel = $this->loadModel('Event');
		$eventModel->updateEvent($eid, array('changed'=>time(), 'bookmark_count'=>$total));
		$this->clearEventBookmarkCache($eid);
	}
	
	private function addEventBookmarks($eid, $bids, $uid){
		$eid = (int)$eid;
		$uid = (int)$uid;
		for($i=0;$i<count($bids); $i++){
			$this->_helper->bookmark->addEventBookmark($eid, $bids[$i], $uid);
		}
		
		$total = $this->_helper->bookmark->getEventBookmarkCount($eid);
		$eventModel = $this->loadModel('Event');
		$eventModel->updateEvent($eid, array('changed'=>time(), 'bookmark_count'=>$total));
		$this->clearEventBookmarkCache($eid);
	}
	
	private function clearEventBookmarkCache($eid){
		$this->deleteCache("html_event_bookmark_block_".$eid);
	}
}

